<?php

namespace App\Logic\finger;

use App\Logic\Code;
use App\Models\MinerInfo;
use App\Models\User;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;


/**
 * @author AdamTyn
 * @description <用户>数据模型
 */
class MinerBalanceLogic
{


    //添加
    public static function transfer($data,$user,$scene){

        $validateRule = [
            'amount' => 'required',
            'mid' => 'required',
            'code' => 'required',
            'token' => 'required',
        ];
        $errorMsg = [
            'mid.required' => '请传入矿机id mid',
            'amount.required' => '请传入划转数量 amount',
            'code.required' => '请传入验证码 code',
            'token.required' => '请传入币种信息 FIL',
        ];

        $validator = Validator::make($data, $validateRule, $errorMsg);
        if ($validator->fails()) {
            $errors = $validator->errors()->all();
            return e_msg(300001, $errors[0]);
        }

        $key = $user['phone'] . '_code_' . $scene;
        $code_info = Code::code($key, $data['code']);
        if ($code_info['status'] === 'ERROR') {
//            return $code_info;
        }

        $miner_user_name = DB::table('miner_user')
            ->where('mid',$data['mid'])
            ->value('user_name');
        if(!$miner_user_name){
            return e_msg(300001,'该矿机不是您的');
        }

        $rel_amount = DB::table('miner_balance')
            ->where('token', $data['token'])
            ->where('mid', $user['mid'])
            ->sum('amount');

        if ($rel_amount < $data['amount']) {
//            return e_msg(300026, '划转余额不足');
        }

        $balance_insert_arr = [
            'token' => $data['token'],
            'mid' => $data['mid'],
            'amount' => '-'.$data['amount'],
            'ticket' => 'data_transfer_' . date('YmdHis'),
            'type' => 'transfer',
            'type_note' => '转出',
        ];

        $fil_insert_arr = [
            'out_account' => $user['username'],
            'out_phone' => $user['phone'],
            'shift_account' => '自己',
            'shift_phone' => '自己',
            'amount' => $data['amount'],
            'type_note' => '用户资金划转',
            'type' => 'miner_transfer',
        ];

        //用户表添加fil，矿机钱包表记录，fil表记录
        DB::beginTransaction();

        $rel1 = DB::table('user')->where('id',$user['id'])->increment('fil',$data['amount']);
        $rel2 = DB::table('miner_balance')->insert($balance_insert_arr);
        $rel3 = DB::table('record_fil')->insert($fil_insert_arr);

        if($rel1&&$rel2&&$rel3){
            DB::commit();
            return s_msg('');
        }else{
            DB::rollBack();
            return e_msg(123456,'有问题');
        }

    }

    //详情
    public static function xq($data){


        $validateRule = [
            'mid' => 'required',
            'token' => 'required',
        ];
        $errorMsg = [
            'mid.required' => '请传入矿机信息',
            'token.required' => '请传入币种信息',
        ];

        $validator = Validator::make($data, $validateRule, $errorMsg);
        if ($validator->fails()) {
            $errors = $validator->errors()->all();
            return e_msg(300001, $errors[0]);
        }

        $host = DB::table('miner_host')
            ->where('mid',$data['mid'])
            ->select('add_time','start_time','end_time','hspace')
            ->first();
        if(!$host){
            return e_msg(123456,'矿机不存在');
        }
        $m_number = DB::table('miner_info')->where('mid',$data['mid'])->value('m_number');

        //总收益，已提现，剩余
        $rel = DB::table('miner_balance')
            ->where('mid',$data['mid'])
            ->where('token',$data['token'])
            ->sum('amount');

        $already = DB::table('miner_balance')
            ->where('mid',$data['mid'])
            ->where('token',$data['token'])
            ->whereIn('type',['transfer'])
            ->sum('amount');

        $total = $rel - $already;
        $arr = [
            // 'total' => $total,
            // 'already' => abs($already),
            // 'rel' => $rel,
            //保留6位小数
            'total' => number_format($total , 6),
            'already' => number_format(abs($already) , 6),
            'rel' => number_format($rel ,6),
            'start_time' =>  date('Y-m-d',$host->start_time),
            'end_time' =>  date('Y-m-d',$host->end_time),
            'm_number' => $m_number,
            'hspace' => $host->hspace,

        ];
        return s_msg('',$arr);





    }

}
